package com.bocai.eelly.activitys;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.FindClothingStockAdp;
import com.bocai.eelly.adapter.FindFuLiaoStockAdp;
import com.bocai.eelly.adapter.FindMianLiaoStockAdp;
import com.bocai.eelly.adapter.FindToolsStockAdp;
import com.bocai.eelly.bean.FindClothStockBean;
import com.bocai.eelly.bean.FindFabricStockBean;
import com.bocai.eelly.bean.FindFuliaoStockBean;
import com.bocai.eelly.bean.FindToolsStockBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.SP;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.bocai.eelly.views.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 *搜索库存
 */
public class SearchStockActivity extends BaseActivity implements XListView.IXListViewListener,AdapterView.OnItemClickListener{
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.search_cancle)
    Button searchCancle;
    @Bind(R.id.search_list)
    XListView searchList;
    private int page=1;
    private int what;
    private List<FindToolsStockBean.DataEntity> toolsStockBeans=new ArrayList<>();
    private List<FindClothStockBean.DataEntity> clothStockBeans=new ArrayList<>();
    private List<FindFabricStockBean.DataEntity> fabricStockBeans=new ArrayList<>();
    private List<FindFuliaoStockBean.DataEntity> fuliaoStockBeans=new ArrayList<>();
    private FindClothingStockAdp findClothingStockAdp;
    private FindToolsStockAdp findToolsStockAdp;
    private FindMianLiaoStockAdp findMianLiaoStockAdp;
    private FindFuLiaoStockAdp findFuLiaoStockAdp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        what=getIntent().getIntExtra("stock",0);
        initView();
        initEvent();
    }

    private void initEvent() {
        searchList.setXListViewListener(this);
        searchList.setPullLoadEnable(false);
        searchList.setPullRefreshEnable(true);
        searchList.setOnItemClickListener(this);
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    page=1;
                    searchList.setPullLoadEnable(false);
                    switch (what) {
                        case 0:
                            clothStockBeans.clear();
                            getClothStock();
                            break;
                        case 1:
                            fabricStockBeans.clear();
                            getMianliaoStock();
                            break;
                        case 2:
                            fuliaoStockBeans.clear();
                            getFuliaoStock();
                            break;
                        case 3:
                            toolsStockBeans.clear();
                            getToolsStock();
                            break;
                    }


                }
                return false;
            }
        });
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_search, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        switch (what) {
            case 0:
                findClothingStockAdp=new FindClothingStockAdp(this,clothStockBeans);
                searchList.setAdapter(findClothingStockAdp);
                break;
            case 1:
                findMianLiaoStockAdp=new FindMianLiaoStockAdp(this,fabricStockBeans);
                searchList.setAdapter(findMianLiaoStockAdp);
                break;
            case 2:
                findFuLiaoStockAdp=new FindFuLiaoStockAdp(this,fuliaoStockBeans);
                searchList.setAdapter(findFuLiaoStockAdp);
                break;
            case 3:
                findToolsStockAdp=new FindToolsStockAdp(this,toolsStockBeans);
                searchList.setAdapter(findToolsStockAdp);
                break;
        }

    }
    private void getToolsStock() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        String id= SP.get(this,"id","0").toString();
        getTestApi().findToolsStock(timeline,sign,id,page,Utils.PAGESIZE,etSearch.getText().toString())
                .compose(this.<FindToolsStockBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FindToolsStockBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        searchList.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showLong(SearchStockActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(FindToolsStockBean findToolsStockBean) {
                        L.d(findToolsStockBean.getMsg());
                        searchList.stopRefresh();
                        Loading.dismiss();
                        if (Utils.checkResultCode(findToolsStockBean.getReturn_code())) {
                            toolsStockBeans.addAll(findToolsStockBean.getData());
                            findToolsStockAdp.notifyDataSetChanged();
                            if (findToolsStockBean.getData().size() < Utils.PAGESIZE) {
                                searchList.setPullLoadEnable(false);
                            }else{
                                searchList.setPullLoadEnable(true);
                            }
                        }else{
                            T.showShort(SearchStockActivity.this,findToolsStockBean.getMsg());
                        }
                    }
                });
    }
    private void getMianliaoStock() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        String id= SP.get(this,"id","0").toString();
        getTestApi().findMianliaoStock(timeline,sign,id,page,Utils.PAGESIZE,etSearch.getText().toString())
                .compose(this.<FindFabricStockBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FindFabricStockBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        searchList.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showLong(SearchStockActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(FindFabricStockBean findFabricStockBean) {
                        L.d(findFabricStockBean.getMsg());
                        searchList.stopRefresh();
                        Loading.dismiss();
                        if (Utils.checkResultCode(findFabricStockBean.getReturn_code())) {
                            fabricStockBeans.addAll(findFabricStockBean.getData());
                            findMianLiaoStockAdp.notifyDataSetChanged();
                            if (findFabricStockBean.getData().size() < Utils.PAGESIZE) {
                                searchList.setPullLoadEnable(false);
                            }else{
                                searchList.setPullLoadEnable(true);
                            }
                        }else{
                            T.showShort(SearchStockActivity.this,findFabricStockBean.getMsg());
                        }

                    }
                });
    }

    private void getFuliaoStock() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        String id= SP.get(this,"id","0").toString();
        getTestApi().findFuliaoStock(timeline,sign,id,page,Utils.PAGESIZE,etSearch.getText().toString())
                .compose(this.<FindFuliaoStockBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FindFuliaoStockBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        searchList.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showLong(SearchStockActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(FindFuliaoStockBean findFuliaoStockBean) {
                        L.d(findFuliaoStockBean.getMsg());
                        searchList.stopRefresh();
                        Loading.dismiss();
                        if (Utils.checkResultCode(findFuliaoStockBean.getReturn_code())) {
                            fuliaoStockBeans.addAll(findFuliaoStockBean.getData());
                            findFuLiaoStockAdp.notifyDataSetChanged();
                            if (findFuliaoStockBean.getData().size() < Utils.PAGESIZE) {
                                searchList.setPullLoadEnable(false);
                            }else{
                                searchList.setPullLoadEnable(true);
                            }
                        }else{
                            T.showShort(SearchStockActivity.this,findFuliaoStockBean.getMsg());
                        }
                    }
                });
    }


    private void getClothStock() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        String id= SP.get(this,"id","0").toString();
        getTestApi().findClothStock(timeline,sign,id,page,Utils.PAGESIZE,etSearch.getText().toString())
                .compose(this.<FindClothStockBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FindClothStockBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        searchList.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showLong(SearchStockActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(FindClothStockBean findClothStockBean) {
                        L.d(findClothStockBean.getMsg());
                        searchList.stopRefresh();
                        Loading.dismiss();
                        if (Utils.checkResultCode(findClothStockBean.getReturn_code())) {
                            clothStockBeans.addAll(findClothStockBean.getData());
                            findClothingStockAdp.notifyDataSetChanged();
                            if (findClothStockBean.getData().size() < Utils.PAGESIZE) {
                                searchList.setPullLoadEnable(false);
                            }else{
                                searchList.setPullLoadEnable(true);
                            }
                        }else{
                            T.showShort(SearchStockActivity.this,findClothStockBean.getMsg());
                        }
                    }
                });
    }
    @Override
    public void onRefresh() {
        switch (what) {
            case 0:
                page=1;
                clothStockBeans.clear();
                searchList.setPullLoadEnable(false);
                getClothStock();
                break;
            case 1:
                page=1;
                fabricStockBeans.clear();
                searchList.setPullLoadEnable(false);
                getMianliaoStock();
                break;
            case 2:
                page=1;
                fuliaoStockBeans.clear();
                searchList.setPullLoadEnable(false);
                getFuliaoStock();
                break;
            case 3:
                page=1;
                toolsStockBeans.clear();
                searchList.setPullLoadEnable(false);
                getToolsStock();
                break;
        }
    }

    @Override
    public void onLoadMore() {
        switch (what) {
            case 0:
                page++;
                getClothStock();
                break;
            case 1:
                page++;
                getMianliaoStock();
                break;
            case 2:
                page++;
                getFuliaoStock();
                break;
            case 3:
                page++;
                getToolsStock();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}